import rankingStore from "../../store/ranking";
import recommendStore from "../../store/recommend";
import { getSongRecommend } from "../../service/request/main"
// pages/mainDetail/mainDetail.js
Page({
  data: {
    songs: {},
    type: "",
    key: "",
    id: 0
  },
  onLoad(options) {
    if(options.type === "ranking") {
      this.data.key = options.key
      this.setData({type: options.type})
      rankingStore.onState(options.key, this.handleRanking)
    } else if(options.type === "recommend") {
      this.setData({type: options.type})
      recommendStore.onState("recommend", this.handleRanking)
    } else if(options.type === "menu") {
      this.setData({type: options.type})
      this.data.id = options.id
      this.fetchMenuInfo()
      wx.setNavigationBarTitle({
        title: '歌单',
      })
    }
  },
  handleRanking(value) {
    this.setData({songs: value})
    wx.setNavigationBarTitle({
      title: value.name,
    })
  },
  async fetchMenuInfo() {
    const id = this.data.id
    const res = await getSongRecommend(id)
    this.setData({songs: res.playlist})
  },
  onUnload() {
    if(this.data.type === "ranking") {
      rankingStore.offState(this.data.key, this.handleRanking)
    } else if (this.data.type === "recommend") {
      recommendStore.offState("recommend", this.handleRanking)
    }
  }
})